<div id="main">
    <div id="user-center-sidebar-left">
        <?php $this->load->view('home/user-center/sidebar'); ?>
    </div>
    <div id="user-center-body">
        <div id="msg-all">
            <?php if (isset($comments) && is_array($comments)): ?>
                <?php foreach ($comments as $comment): ?>
                    <?php /** @var UserComment $comment */ ?>
                    <div class="msg-list">
                        <a href="" class="msg-list-portrait"><img src="<?php echo $comment->getUserDisplayAvatarUrl() ?>"/></a>

                        <div class="msg-list-con">
                            <div class="msg-list-title">
                                <div class="msg-list-username"><a href="<?php echo base_url('home/userCenter/' . $comment->getUserId()) ?>"><?php echo $comment->getUserDisplayName() ?></a></div>
                                <?php if ($comment->getBigType() == 0): ?>
                                    <span>在</span> <a href="<?php echo base_url('home/details/' . $comment->getCommodityIdOrCommodityRequireId()) ?>">《<?php echo $comment->getLinkDisplayTitle() ?>》</a>
                                <?php else : ?>
                                    <span>在</span> <a href="javascript:">《<?php echo $comment->getLinkDisplayTitle() ?>》</a>
                                <?php endif; ?>
                                <span style="color:#FF6347;">[<?php echo $comment->getCommentDisplayTypeStr() ?>]：</span>@<?php echo $comment->getToUserDisplayName() ?>
                                <div class="msg-list-time"><?php echo $comment->getReleaseTime() ?></div>
                            </div>

                            <div class="msg-list-info">
                                <?php if ($comment->getType() == _UserCommentType_HuiFu): ?>
                                    <p class="msg-list-info-ed">
                                        <?php echo $comment->getFatherCommentDisplayContent() ?>
                                    </p>
                                <?php endif; ?>

                                <p>
                                    <?php echo $comment->getCommentDisplayContent() ?>
                                </p>

                                <div class="revert">

                                    <a class="del_btn2" data-comment-big-type="<?php echo $comment->getBigType() ?>" data-comment-id="<?php echo $comment->getCommentId() ?>" href="javascript:">删除</a>
                                    <a class="rev_btn2" href="javascript:">回复</a>

                                    <div class="cb"></div>
                                </div>
                            </div>

                            <form class="leave-commodity-comment" method="post" style="display: none;">
                                <div class="form-content" style="overflow: hidden">
                                    <input type="hidden" name="commodity_id" value="<?php echo $comment->getCommodityIdOrCommodityRequireId() ?>">
                                    <input type="hidden" name="father_id" value="<?php echo $comment->getCommentId() ?>">

                                    <div class="input-group">
                                        <label class="input-group-addon sr-only">回复</label>
                                        <textarea name="content" class="form-control reply-textarea" placeholder="请输入回复内容" required="required"></textarea>
                                    </div>
                                    <div class="pull-right">
                                        <input type="checkbox" name="private" style="vertical-align:-2px"/>
                                        <span class="secret-txt">密谈</span>
                                        <input type="submit" class="btn btn-primary" value="发表"/>
                                    </div>
                                </div>
                            </form>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
            <div id="pagination">
                <?php if (isset($commentsPagination)) echo $commentsPagination; ?>
            </div>
        </div>
    </div>
</div>
<script>
    $(document).ready(function () {
        function hideAllLeaveCommentForm() {
            $('form.leave-commodity-comment').slideUp();
        }

        $(document).on('click', '.rev_btn2', function () {
            hideAllLeaveCommentForm();
            var $msgList = $(this).closest('.msg-list');
            var $comment = $msgList.find('.leave-commodity-comment');
            if ($comment.css('display') == 'none') {
                $comment.slideDown('fast');
                $(this).css('color', 'rgb(255, 250, 148)');
            } else {
                $comment.slideUp('fast');
                $(this).removeAttr('style');
            }
        });
        $(document).on('click', '.del_btn2', function () {
            var id = $(this).data('comment-id');
            var bigType = $(this).data('comment-big-type');
            switch (bigType) {
                case 0:
                    noty({
                        type: 'warning',
                        text: '确定要删除吗？',
                        buttons: [
                            {
                                addClass: 'btn btn-sm btn-danger', text: '确定',
                                onClick: function ($noty) {
                                    $noty.close();
                                    $.ajax({
                                        url: base_url('api/deleteCommodityComment'),
                                        type: 'post',
                                        data: {commodity_comment_id: id},
                                        beforeSend: function () {
                                            showProgressNoty();
                                        },
                                        complete: function () {
                                            closeProgressNoty();
                                        },
                                        success: function (jsonrsp) {
                                            try {
                                                var rsp = $.parseJSON(jsonrsp);
                                                noty({type: rsp.type, text: rsp.message, callback: {
                                                    afterClose: function () {
                                                        switch (rsp.type) {
                                                            case 'success':
                                                                location.reload();
                                                                break;
                                                            default:
                                                                break;
                                                        }
                                                    }
                                                }});
                                            } catch (ex) {
                                                noty({type: 'error', text: '发生内部错误，请联系管理员'});
                                                throw ex;
                                            }
                                        }
                                    });
                                }
                            },
                            {
                                addClass: 'btn btn-sm btn-default', text: '取消',
                                onClick: function ($noty) {
                                    $noty.close();
                                }
                            }
                        ]
                    });
                    break;
                case 1:
                    noty({
                        type: 'warning',
                        text: '确定要删除吗？',
                        buttons: [
                            {
                                addClass: 'btn btn-sm btn-danger', text: '确定',
                                onClick: function ($noty) {
                                    $noty.close();
                                    $.ajax({
                                        url: base_url('api/deleteCommodityRequireComment'),
                                        type: 'post',
                                        data: {commodity_require_comment_id: id},
                                        beforeSend: function () {
                                            showProgressNoty();
                                        },
                                        complete: function () {
                                            closeProgressNoty();
                                        },
                                        success: function (jsonrsp) {
                                            try {
                                                var rsp = $.parseJSON(jsonrsp);
                                                noty({type: rsp.type, text: rsp.message, callback: {
                                                    afterClose: function () {
                                                        switch (rsp.type) {
                                                            case 'success':
                                                                location.reload();
                                                                break;
                                                            default:
                                                                break;
                                                        }
                                                    }
                                                }});
                                            } catch (ex) {
                                                noty({type: 'error', text: '发生内部错误，请联系管理员'});
                                                throw ex;
                                            }
                                        }
                                    });
                                }
                            },
                            {
                                addClass: 'btn btn-sm btn-default', text: '取消',
                                onClick: function ($noty) {
                                    $noty.close();
                                }
                            }
                        ]
                    });
                    break;
                default :
                    break;
            }
        });

        $(document).on('submit', 'form.leave-commodity-comment', function (event) {
            event.preventDefault();
            var ifpower = $("#ifpower").val();
            if (ifpower == 0) {
                noty({
                    type: 'warning',
                    text: '请先激活邮箱帐号，并进行实名认证，Lets Go？',
                    buttons: [
                        {
                            addClass: 'btn btn-sm btn-danger', text: '确定',
                            onClick: function ($noty) {
                                $noty.close();
                                window.location.href = base_url('home/userCenter#resendemail');
                            }
                        },
                        {
                            addClass: 'btn btn-sm btn-default', text: '取消',
                            onClick: function ($noty) {
                                $noty.close();
                            }
                        }
                    ]
                });
            }
            else {
                var form = this;
                var fatherName = $(form).data('father-name');
                var masterCommentId = $(form).data('master-comment-id');
                $.ajax({
                    url: base_url('api/leaveCommodityComment'),
                    type: 'post',
                    data: $(form).serialize(),
                    success: function (jsonrsp) {
                        try {
                            var rsp = $.parseJSON(jsonrsp);
                            switch (rsp.type) {
                                case 'success':
                                    noty({type: rsp.type, text: rsp.message});
                                    hideAllLeaveCommentForm();
                                    break;
                                default:
                                    noty({type: rsp.type, text: rsp.message});
                                    break;
                            }
                        } catch (ex) {
                            noty({type: 'error', text: '发生内部错误，请联系管理员'});
                            throw ex;
                        }
                    }
                });
            }
        });
    });
</script>